<template>
  <div>
    <van-nav-bar class="navbar">
      <template #title>
        <van-button 
        icon="search" 
        plain type="info" 
        round 
        class="search-btn"
        @click="$router.push('./search')"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <van-tabs v-model="active" swipeable>
      <van-tab v-for="item in myChannels" :key="item.id" :title="item.name">
        <article-list :channel-id="item.id"></article-list>
      </van-tab>
      <span class="toutiao toutiao-gengduo" @click="isshowEdit = true"></span>
    </van-tabs>
    <!-- 频道编辑 -->
    <van-popup
      v-model="isshowEdit"
      position="bottom"
      :style="{
        height: '100%',
        width: '100%'
      }"
      closeable
      close-icon-position="top-left"
    >
      <edit-popup
        :my-channels="myChannels"
        @delete-channel="delMyChannel"
        @add-channel="addMyChannel"
        @updata-active=";[(active = $event), (isshowEdit = false)]"
      ></edit-popup>
    </van-popup>
  </div>
</template>

<script>
import { getchannelAPI, delMyChannelAPI, addMyChannel } from '@/api'
import ArticleList from './components/ArticleList'
import EditPopup from './components/EditPopup.vue'
import { mapGetters, mapMutations } from 'vuex'
export default {
  name: 'Home',
  components: {
    ArticleList,
    EditPopup
  },
  data() {
    return {
      active: 0,
      myChannels: [], //所有的频道
      // 这里我们先设置为 true 就能看到弹窗的页面了
      isshowEdit: false
    }
  },
  created() {
    this.initMyChannels()
  },
  computed: {
    ...mapGetters(['isLogin'])
  },
  methods: {
    ...mapMutations(['SET_MY_CHANNELS']),
    async initMyChannels() {
      if (this.isLogin) {
        this.getMyChannels()
      } else {
        const { myChannels } = this.$store.state
        if (myChannels.length > 0) {
          this.myChannels = myChannels
        } else {
          await this.getMyChannels()
          this.SET_MY_CHANNELS(this.myChannels)
        }
      }
    },
    // 获取我的频道
    async getMyChannels() {
      const { data } = await getchannelAPI()
      this.myChannels = data.data.channels
    },
    async delMyChannel(id) {
      try {
        this.$toast.loading({
          message: '正在删除频道...',
          forbidClick: true
        })
        //从我的频道的数组里筛选掉
        const newChannels = this.myChannels.filter((item) => item.id !== id)
        if (this.isLogin) {
          await delMyChannelAPI(id)
        } else {
          console.log(1)
          //如果没有登录,将删除好的我的频道的数组存到本地
          this.SET_MY_CHANNELS(newChannels)
        }

        // this.myChannels = this.myChannels.filter((item) => item.id !== id);
        //在视图层上删除频道
        this.myChannels = newChannels
        this.$toast.success('删除成功')
      } catch (error) {
        this.$toast.fail('删除失败')
      }
    },
    async addMyChannel(myChannel) {
      try {
        this.$toast.loading({
          message: '正在删除频道...',
          forbidClick: true
        })
        if (this.isLogin) {
          await addMyChannel(myChannel.id, this.myChannels.length)
        } else {
          //如果没有登录,将删除好的我的频道的数组存到本地
          this.SET_MY_CHANNELS([...this.myChannels, myChannel])
        }
        //2.在视图层上添加频道
        this.myChannels.push(myChannel)
        //3.提示添加成功
        this.$toast.success('添加频道成功')
      } catch (error) {
        this.$toast.fail('添加失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
:deep(.navbar) {
  background-color: #3296fa;

  .van-nav-bar__title {
    max-width: unset;
  }

  .van-icon-search {
    color: #fff;
  }

  .search-btn {
    width: 7.4rem;
    height: 0.85333rem;
    color: #fff;
    background-color: #5babfb;
    border: unset;
  }
}
/* tabs导航条样式 */
:deep(.van-tabs__wrap) {
  padding-right: 66px;
  .van-tabs__nav {
    padding-left: 0;
    padding-right: 0;

    /* tab标签 */
    .van-tab {
      border: 1px solid #eee;
      width: 200px;
      height: 82px;
    }
    /* tab标签下划线 */
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 40px;
    }
  }
}

/* 字体图标 */
.toutiao-gengduo {
  position: absolute;
  top: 0;
  right: 0;
  width: 66px;
  height: 82px;
  font-size: 40px;
  line-height: 82px;
  text-align: center;
  opacity: 0.6;
  border-bottom: 1px solid #eee;

  &::after {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 70%;
    width: 1px;
    background-image: url('~@/assets/images/gradient-gray-line.png');
  }
}
</style>
